/*
* wx
*/
import { APP_ID, BASE_API_URL } from '../config'
export default {
  init: function () {
    const request = wx.request;
    Object.keys(wx).forEach(key => {
      if (typeof wx[ key ] === 'function' && !/Sync$/.test(key) && !/createAnimation/.test(key)
        && !/^on/.test(key) && !/^start/.test(key) &&  !/^connectWifi/.test(key) && !/^getWifiList/.test(key) && !/^create/.test(key)) {
        const tempFunc = wx[ key ];
        Object.defineProperty(wx, key, {
          value(config) {
            config = config || {};
            return new Promise(function (resolve, reject) {
              let success = config.success, fail = config.fail;
              config.success = function (result) {
                console.log(result)
                resolve(result)
                success && success(result)
              }
              config.fail = function (result) {
                console.log(result)
                resolve(result)
                fail && fail(result)
              }
              tempFunc(config)
            });
          },
          writable: true
        })
      }
    })
    Object.defineProperty(wx, 'request', {
      value(config) {
        config = config || {};
        return new Promise(function (resolve, reject) {
          if (!/^http/.test(config.url)) {
            config.url = BASE_API_URL + config.url;
          }
          let success = config.success, fail = config.fail;
          config.header = Object.assign({
            'X-Gizwits-Application-Id': APP_ID,
          }, config.header)
          config.success = function (result) {
            success && success(result.data)
            resolve(result.data)
          }
          config.fail = function (result) {
            fail && fail(result)
            resolve(result)
          }
          request(config)
        });
      },
      writable: true
    })
  }
}
